using System;
using Guestbook.domain;
using System.Collections;
using Guestbook.dao;
using System.Collections.Generic;
using System.Web.Configuration;

namespace Guestbook.business.service
{
    public static class GuestBookService
    {
        #region - Provider model -

        private static GuestBookProvider _provider;
        private static GuestBookProviderCollection _providers;
        private static object _lock = new object();

        /// <summary>
        /// Gets the current provider.
        /// </summary>
        public static GuestBookProvider Provider
        {
            get { LoadProviders(); return _provider; }
        }

        /// <summary>
        /// Load the providers from the web.config.
        /// </summary>
        private static void LoadProviders()
        {
            // Avoid claiming lock if providers are already loaded
            if (_provider == null)
            {
                lock (_lock)
                {
                    // Do this again to make sure _provider is still null
                    if (_provider == null)
                    {
                        // Get a reference to the <blogProvider> section
                        GuestBookProviderSection section = (GuestBookProviderSection)WebConfigurationManager.GetSection("GuestBookEngine/guestBookProvider");

                        // Load registered providers and point _provider
                        // to the default provider
                        _providers = new GuestBookProviderCollection();
                        ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(GuestBookProvider));
                        _provider = _providers[section.DefaultProvider];

                        if (_provider == null)
                            throw new Exception("Unable to load default GuestBookProvider");
                    }
                }
            }
        }

        #endregion

        public static void SendMail(string from, string recipients, string subject, string body)
        {
            System.Net.Mail.MailMessage message 
                = new System.Net.Mail.MailMessage(from, recipients, subject, body);
            SendMail(message);
        }

        public static void SendMail(System.Net.Mail.MailMessage message)
        {
            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
            smtpClient.Host = "localhost";
            smtpClient.Port = 25;

            smtpClient.Send(message);
        }

        #region - GuestBook -

        public static GuestBook GetGuestBook(int id, int pageSize, int currentPage)
        {
            return GuestBookService.Provider.GetGuestBook(id, pageSize, currentPage);
        }

        public static GuestBook GetSelfGuestBookById(int id)
        {
            return GuestBookService.Provider.SelectSelfGuestBookById(id);
        }

        public static GuestBook GetSelfGuestBookByEmail(string email)
        {
            return GuestBookService.Provider.SelectSelfGuestBookByEmail(email);
        }

        public static GuestBook GetGuestBookById(int id)
        {
            return GuestBookService.Provider.SelectGuestBookById(id);
        }

        public static int AddGuestBook(GuestBook obj)
        {
            return GuestBookService.Provider.InsertGuestBook(obj);
        }

        public static void DeleteGuestBookById(int id)
        {
            GuestBookService.Provider.DeleteGuestBook(id);
        }

        public static void UpdateGuestBook(GuestBook obj)
        {
            GuestBookService.Provider.UpdateGuestBook(obj);
        }

        public static bool IsExist(int id)
        {
            return GuestBookService.Provider.IsExist(id);
        }

        #endregion

        #region - Message -

        public static Message SelectMessagesById(int id)
        {
            return GuestBookService.Provider.SelectMessagesById(id);
        }

        public static void UpdateMessages(Message message)
        {
            GuestBookService.Provider.UpdateMessage(message);
        }

        public static void InsertMessage(Message message)
        {
            GuestBookService.Provider.InsertMessage(message);
        }

        public static void DeleteMessagesById(int id)
        {
            GuestBookService.Provider.DeleteMessagesById(id);
        }

        #endregion
    }
}
